//
//  NSDateFormatter+CacheDateFormatter.m
//  WanCaiC8
//
//  Created by 罗先森 on 2018/9/14.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "NSDateFormatter+CacheDateFormatter.h"

@implementation NSDateFormatter (CacheDateFormatter)


static NSDateFormatter *cachedDateFormatter = nil;

//+ (NSDateFormatter *)cachedDateFormatter {
//
//    // If the date formatters aren't already set up, create them and cache them for reuse.
//
//    if (!dateFormatter) {
//
//        dateFormatter = [[NSDateFormatter alloc] init];
//
//        [dateFormatter setLocale:[NSLocale currentLocale]];
//
//        [dateFormatter setDateFormat: @"YYYY-MM-dd HH:mm:ss"];
//
//    }
//
//    return dateFormatter;
//
//}

//时间戳转字符串


//字符串转Data
+(NSDate *) easyDateFormatterWithIso8601String:(NSString*)str{
    

    
////    %m-%d %H:%M
////    yyyy-MM-dd'T'HH:mm:ss
//    time_t t;
//
//    struct tm tm;
//
////    NSString *iso8601String = @"2016-09-18T17:30:08+08:00";
//
//    //%Y-%m-%d
//    const char *iso8601 = [str cStringUsingEncoding:NSUTF8StringEncoding];
//
////    char *iso8601 = "2018-09-13T20:34:15";
//
//    strptime(iso8601, "%m-%d %H:%M", &tm);
//
//
////    tm.tm_isdst = -1;
////
////    tm.tm_hour = 0;//当tm结构体中的tm.tm_hour为负数，会导致mktime(&tm)计算错误
//
//    tm.tm_sec = 0;
//
//    tm.tm_min = 0;
//
//    tm.tm_hour = 0;
//
//    tm.tm_mday = 18;
//
//    tm.tm_mon = 9;
//
//    tm.tm_year = 116;
//
//    tm.tm_wday = 2;
//
//    tm.tm_yday = 291;
//
//    tm.tm_isdst = 0;
//
//    tm.tm_gmtoff = 28800;
//
//    tm.tm_zone = 0x00007fd9b600c31c "CST";
//
//    /**
//
//     //NSString *iso8601String = @"2016-09-18T17:30:08+08:00";
//
//     //%Y-%m-%d [iso8601String cStringUsingEncoding:NSUTF8StringEncoding]
//
//     {
//
//     tm_sec = 0
//
//     tm_min = 0
//
//     tm_hour = 0
//
//     tm_mday = 18
//
//     tm_mon = 9
//
//     tm_year = 116
//
//     tm_wday = 2
//
//     tm_yday = 291
//
//     tm_isdst = 0
//
//     tm_gmtoff = 28800
//
//     tm_zone = 0x00007fd9b600c31c "CST"
//
//     }
//
//     ISO8601时间格式：2004-05-03T17:30:08+08:00 参考Wikipedia
//
//     */
//
//    t = mktime(&tm);
//
//    //http://pubs.opengroup.org/onlinepubs/9699919799/functions/mktime.html
//
//    //secondsFromGMT: The current difference in seconds between the receiver and Greenwich Mean Time.
//
//    return [NSDate dateWithTimeIntervalSince1970:t + [[NSTimeZone localTimeZone] secondsFromGMT]];


    if (!str) return nil;
    
    
    
    struct tm tm;
    
    time_t t;
    
    
    
    strptime([str cStringUsingEncoding:NSUTF8StringEncoding], "%Y-%m-%dT%H:%M:%S%z", &tm);
    
    tm.tm_isdst = -1;
    
    t = mktime(&tm);
    
    
    
        return [NSDate dateWithTimeIntervalSince1970:t]; // 零时区
    
//    return [NSDate dateWithTimeIntervalSince1970:t + [[NSTimeZone localTimeZone] secondsFromGMT]];

}
+(NSString *)dateFormatterWithRestrictStr:(NSString *)restrictStr TimeStamp:(NSString *)timeSp{
    NSInteger i = [timeSp integerValue];
    
    //    MM-dd HH:mm
    const char *cStr = [restrictStr UTF8String];
    time_t t;
    struct tm *p;
    t=i;
    p=gmtime(&t);
    char s[100];
    
//    strftime(s, sizeof(s), cStr, p);
    strftime(s, sizeof(s), "%m-%d %H:%M", p);

    printf("%d: %s\n", (int)t, s);
    
    
    return  [NSString stringWithUTF8String:s];
    
}


@end
